/** 
 * 
 * A secondary AI class which was
 * created incase the other failed
 * 
 * But it didnt so its rather redundant.
 * 
 * But this is how it works:
 * 
 * It fires, checks where it hit, and changes
 * ammo depending on whether it was an obsticle
 * in which case it uses terrain blasting ammo,
 * 
 * or whether it just landed short/far and then needs
 * to adjust.
 * 
 * Im not updating the comments because they arnt too bad,
 * and its not really used.
 * 
 * @author Andrew
 * @creator Andrew
 *
 */

public class AI {
	
	
	// The actual catapult the AI controls
	private TankObject me;
	
	// The lastlastPowerused
	private double lastPower;
	
	// The last lastAngle it used
	private double lastAngle;
	
	// Did it hit the target last time?
	private boolean lastHit;
	
	// Last Hit X position
	private int lastHitX;
	
	// Last Hit Y position
	private int lastHitY;
	
	// Power to use
	private double newPower;
	
	// Angle to use
	private double newAngle;
	
	// Ammo to use
	private int ammo;
	
	// Target
	private SimpleObject target;
	
	public AI(TankObject tank) {
		this.me = tank;
		this.ammo = 0;
		this.lastAngle = 0;
		this.lastPower = 15;
		this.lastHitY = 232;
		this.lastHitX = 20;
		this.lastHit = true;
		this.newPower = 0;
		this.newAngle = 0;
		this.target = new SimpleObject(0,248,"cata.png");
	}
	
	public int getAmmo() {
		return this.ammo;
	}
	
	public static double powerToTime(double d) {
		double n = (d/15) * 1500;
		return n;
	}
	
	public double getPower() {
		return newPower;
	}
	
	public double getAngle() {
		return this.newAngle;
	}
	
	private void insertHitX(int X) {
		this.lastHitX = X;
	}
	
	private void insertHitY(int Y) {
		this.lastHitY = Y;
	}
	
	private void checkLastHit() {
		this.lastHit = this.target.checkHit(this.lastHitX, this.lastHitY);
	}
	
	public void insert(int x,int y) {
		insertHitX(x);
		insertHitY(y);
		checkLastHit();
	}
	
	public void run() {
		lastPower = newPower;
		lastAngle = newAngle;
		newPower = 0;
		newAngle = 0;
		ammo = 0;
		// If it hit last time, why change?
		if(lastHit) {
			System.out.println("Last hit hit!!!");
			ammo = 3;
			
			if(Math.random() > 0.5) {
				System.out.println("adjusting angle");
				// generate a random 1 or -1
				int random = 1;
				if(Math.random() > 0.5) random = -1;
				// Use the last power
				newPower = lastPower;
				// Use the last angle but +- 0 to 2.5 degrees ( we dont want it hitting all the time now do we ;)     )
				newAngle = lastAngle + ((Math.random() * 0.05) * random);
			}
			else {
				System.out.println("Adjusting power");
				// generate a random 1 or -1
				int random = 1;
				if(Math.random() > 0.5) random = -1;
				// Use the last power
				newPower = lastPower + ((Math.random() * (lastHitX - 20/1000) * 0.01));
				// Use the last angle but +- 0 to 2.5 degrees ( we dont want it hitting all the time now do we ;)     )
				newAngle = lastAngle - ((Math.random()) * random);
			}
			}
		
		else {
			
			if(lastHitY > 400) {
				this.ammo = 1;
				if(Math.random() > 0.8) this.ammo = 4;
				newPower = (newPower*0.1) + newPower;
				newAngle -= 15;
			}
			// If the last Hit hit something that WASNT the ground level, then do it again but with terrain destroying ammo
			else if(lastHitY > 232) {
				System.out.println("Last hit hit too high - firing again with new ammo");
				ammo = 4;
				if(Math.random() < 0.8) this.ammo = 1;
				// generate a random 1 or -1
			
				int random = 1;
				if(Math.random() > 0.5) random = -1;
				// Use the last power
				newPower = lastPower;
				// Use the last angle but +- 0 to 2.5 degrees
				newAngle = lastAngle;
			}
		// If it landed to near
		if(lastHitX > 0) {
			if(Math.random() > 0.5) {
				System.out.println("Too close increasing angle");
				// generate a random 1 or -1
				int random = 1;
				if(Math.random() > 0.5) random = -1;
				// Use the last power but add a bit more power
				newPower = lastPower;
				// Use the last angle but add higher angle
				newAngle = lastAngle + ((Math.random() * 2));
			}
			else {
				System.out.println("Too close increasing power");
				// generate a random 1 or -1
				int random = 1;
				if(Math.random() > 0.5) random = -1;
				// Use the last power but add a bit more power
				newPower = lastPower + ((Math.random() * (lastHitX - 20/1000) * 0.01));
				// Use the last angle but add higher angle
				newAngle = lastAngle;
			}
			}
		}
		
		if(lastHitX < 0) {
			System.out.println("to far decreasing power");
			// generate a random 1 or -1
			int random = 1;
			if(Math.random() > 0.5) random = -1;
			// Use the last power but take away a bit of power
			newPower = lastPower - ((Math.random() * (lastHitX - 20/1000) * 0.01));
			// Use the last angle but lower
			newAngle -= 10;
		}
			
		if(newAngle > 75) {
			newAngle = 75;
			newPower += 1.5;
		}
		if(newPower < 4) {
			newPower = 4;
		}
		if(newPower > 15) {
			newPower = 15;
		}
 }
}
	
